package com.syedu.day03.homework;

import java.util.Scanner;

/**
 * description：猜数字游戏程序
 * @author Administrator
 * @date 2022/3/8 14:57
 */
public class GuessNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //随机生成一个1~100之间的整数
        int randomNumber = (int) (Math.random() * 100 + 1);
        //定义变量保存数值范围
        int start = 1, end = 100;
        //定义变量保存猜测次数
        int count = 0;
        System.out.println("=====猜数字游戏=====");
        System.out.println("请输入你猜的数字(" + start + "~" + end + "):");
        for (; ; ) {
            int inputNumber = scanner.nextInt();
            count++;
            if (count == 6) {
                System.out.println("您的猜测次数以耗尽...");
                break;
            }
            //判断
            if (inputNumber < randomNumber) {
                //说明输入的数字比随机数小,则把起始值改成输入的数字
                start = inputNumber;
                System.out.println("太小了,请输入你猜的数字[" + start + "~" + end + "]:");
            } else if (inputNumber > randomNumber) {
                //说明输入的数字比随机数大,则把结束值改成输入的数字
                end = inputNumber;
                System.out.println("太大了,请输入你猜的数字[" + start + "~" + end + "]:");
            } else {
                //说明猜的数字和随机数相同
                System.out.println("哈哈哈,你中了.");
                //退出循环
                break;
            }
        }
        //打印最终结果
        System.out.println("生成的随机是" + randomNumber + ",你总共猜了" + count + "次.");
    }
}